﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtensionHelper
{
    //基础类型扩展
    public static partial class ExtensionHelper
    {
        #region DateTime
        /// <summary>
        /// 是否最小时间
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsMinDateTime(DateTime date)
        {
            return (DateTime.Compare(date, DateTime.MinValue) == 0
                || date == DateTime.Parse("1/1/1900"));
        }
        #endregion

        #region byte
        /// <summary>
        /// String转Byte[]
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] Str2Bytes(string str)
        {
            return UnicodeEncoding.UTF8.GetBytes(str);
        }

        /// <summary>
        ///String转Byte[],指定编码
        /// </summary>
        /// <param name="str"></param>
        /// <param name="codeName"></param>
        /// <returns></returns>
        public static byte[] Str2Bytes(string str, string codeName)
        {
            return Encoding.GetEncoding(codeName).GetBytes(str);
        }

        /// <summary>
        /// Byte[]转SByte[],Byte:0-255,SByte:-128-127
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static sbyte[] Byte2SByte(byte[] b)
        {
            sbyte[] sb = new sbyte[b.Length];
            for (int i = 0; i < b.Length; i++)
            {
                if (b[i] > 127)
                    sb[i] = (sbyte)(b[i] - 256);
                else
                    sb[i] = (sbyte)b[i];
            }
            return sb;
            //return Array.ConvertAll(b,
            //    (o => (o > 127) ? (sbyte)(o - 256) : (sbyte)o)
            //    );
        }
        /// <summary>
        /// Byte[]转int,可与Int2Byte互相操作
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int Byte2Int(byte[] b)
        {
            return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16 | (b[0] & 0xff) << 24;
        }
        #endregion

        #region int
        /// <summary>
        /// int转Byte[],可与Byte2Int互相操作
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static byte[] Int2Byte(int n)
        {
            byte[] b = new byte[4];
            b[0] = (byte)(n >> 24);
            b[1] = (byte)(n >> 16);
            b[2] = (byte)(n >> 8);
            b[3] = (byte)n;
            return b;
        }

        /// <summary>
        /// 是否最小值-2147483648
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        [DebuggerStepThrough]
        public static bool IsMinInt(int n)
        {
            return n == int.MinValue;
        }
        #endregion

        #region double
        [DebuggerStepThrough]
        public static bool IsMinDouble(double value)
        {
            return value == double.MinValue;
        }
        #endregion

        #region float

        [DebuggerStepThrough]
        public static bool IsMinFloat(float value)
        {
            return value == float.MinValue;
        }

        #endregion
    }

}
